package test.taskengine;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.liusy.jdbc.JdbcHelp;
import com.liusy.taskengine.InitialTaskEngine;
import com.liusy.taskengine.ReleaseTask;
import com.liusy.taskengine.TaskEngineConstant;
import com.liusy.taskengine.TaskManager;
import com.liusy.taskengine.executemodel.IExecuteModel;
import com.liusy.taskengine.resultmodel.ResultModel;
import com.liusy.taskengine.taskmodel.ITaskModel;


/**
 * 测试执行批量SQL任务
 * 
 * @author liusy 2012-12-11
 */
public class CopyOfTestMain {
	
	private static Logger log = Logger.getLogger(CopyOfTestMain.class);
	
	public static void main(String[] args) throws Exception
	{
		try {
		Comparator c = new Comparator<String>(){@Override
			public int compare(String o1, String o2) {
				if(Float.parseFloat(o1.substring(6, o1.length()))<Float.parseFloat(o2.substring(6, o2.length())))
					return -1;
				else if(Float.parseFloat(o1.substring(6, o1.length()))>Float.parseFloat(o2.substring(6, o2.length())))
					return 1;
				return 0;
			}};
			  java.util.Date current=new java.util.Date();
	    java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		System.out.println(sdf.format(current));

		int flag = 0;
		String sql = "";
		String value="";
		//初始化任务引擎
		InitialTaskEngine.initTaskEngine();
		//执行器管理
		TaskManager taskManager= new TaskManager(TaskEngineConstant.ON,new ResultModel("","D:\result.txt"));
		//缓存中的任务少于400监听
		SqlCacheTaskCountListener sqlCacheTaskCountListener= new SqlCacheTaskCountListener(400);
		//发布任务
		ReleaseTask rt = new ReleaseTask(taskManager);
		//添加监听
		rt.addListener(ReleaseTask.CACHE_TASK_COUNT_LISTENER, sqlCacheTaskCountListener);
		
		JdbcHelp jdbcHelp=  JdbcHelp.getJdbcHelp();
		List<Map<String,Object>> list = jdbcHelp.getDataObject("SELECT meter_id from customer_load WHERE rownum<1000", jdbcHelp.getConnection(TaskEngineConstant.QUERY_TASK_CONNECTION));
		
		for (Map<String, Object> map : list) {
			++flag;
			String meterid = map.get("METER_ID").toString();
//			String [] data =map.get("REAL_LOAD").toString().split(",");
//			String runtime = map.get("RUN_TIME").toString();
//			Arrays.sort(data, c );
//			value=data[data.length-1];
//			value = value.substring(6, value.length());			
			//任务模型
			ITaskModel taskModel = new SqlTaskModel(map);
			rt.ExeReleaseTask(taskModel);
		}
		log.debug("总线程数:"+taskManager.threadCount);
		log.debug("正在任务线程数 :"+(taskManager.threadCount-taskManager.countObservers()));
		log.debug("等待:"+taskManager.countObservers());
		rt.start();
    }	    
    catch (Exception ex) {
    	ex.printStackTrace();
    }
	
		
//		BatchExecuteObservable batchExecuteObservable= new BatchExecuteObservable(1);
//		String sql  ="";
//		for (int i = 0; i < 1000000; i++) {
//			//更新操作引擎
//			batchExecuteObservable.getBatchExecute(sql);
//		}
//		batchExecuteObservable.notifyObservers();
		
//		String [] data = "23:00:10000.78399658203125,10:00:74.86799621582031,09:45:59.987998962402344,09:30:50.42399978637695,09:15:59.231998443603516,09:00:66.947998046875,08:45:59.183998107910156,08:30:46.667999267578125,08:15:48.69599914550781,08:00:41.099998474121094,14:15:70.96800231933594,14:00:81.73200225830078,13:45:76.46399688720703,13:30:77.94000244140625,13:15:67.23600006103516,13:00:69.33599853515625,12:45:64.1760025024414,12:30:71.7239990234375,12:15:77.3280029296875,15:45:76.90799713134766,15:30:81.22799682617188,15:15:84.37200164794922,15:00:84.12000274658203,14:45:79.87200164794922,14:30:71.87999725341797,12:00:87.16799926757812,11:45:73.05599975585938,11:30:67.91999816894531,11:15:74.73600006103516,11:00:69.01200103759766,10:45:63.80400085449219,10:30:59.80799865722656,10:15:58.90800094604492,07:45:41.19599914550781,07:30:43.82400131225586,07:15:41.07600021362305,07:00:42.92399978637695,06:45:50.040000915527344,06:30:56.099998474121094,06:15:50.70000076293945,03:45:65.78399658203125,03:30:67.99199676513672,03:15:70.10399627685547,03:00:71.38800048828125,02:45:74.61599731445312,02:30:77.7239990234375,02:15:78.21600341796875,02:00:72.61199951171875,01:45:73.5479965209961,06:00:52.847999572753906,05:45:64.08000183105469,05:30:63.347999572753906,05:15:67.72799682617188,05:00:68.7959976196289,04:45:65.052001953125,04:30:62.86800003051758,04:15:63.492000579833984,04:00:61.69200134277344,00:30:83.052001953125,00:45:76.76399993896484,01:00:79.18800354003906,01:30:79.37999725341797,01:15:72.8759994506836,00:15:84.36000061035156".split(",");
//		Map<String,String> map = new HashMap<String,String>();
//		TreeMap<String, String> treeMap = new TreeMap<String,String>();
//		String beginTime = "" ;    
//		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
//		  Calendar cal= Calendar.getInstance();       
//		  cal.setTime(sdf.parse("00:00"));    
//		for (int i = 0; i < 96; i++) {
//		  cal.add(Calendar.MINUTE, +15);       
//		  beginTime = sdf.format(cal.getTime());  
//		  System.out.println(beginTime) ;
//		  map.put(beginTime, "0");
//		}
//		//java.util.Arrays.sort(data);
//		Arrays.sort(data, new Comparator<String>(){@Override
//		public int compare(String o1, String o2) {
//			// TODO Auto-generated method stub
//			if(Float.parseFloat(o1.substring(6, o1.length()))<Float.parseFloat(o2.substring(6, o2.length())))
//				return -1;
//			else if(Float.parseFloat(o1.substring(6, o1.length()))>Float.parseFloat(o2.substring(6, o2.length())))
//				return 1;
//			return 0;
//		}});
//		System.out.println(data[Arrays.binarySearch(data,"23:00:10000.78399658203125")]); 
//		String vlu="";
//		for (int i = 0; i < data.length; i++) {
//			vlu = data[i];
//			System.out.println(vlu);
//			treeMap.put(vlu.substring(0, 5), vlu.substring(6, vlu.length()));
//			map.put(vlu.substring(0, 5), vlu.substring(6, vlu.length()));
//			//System.out.println(vlu.substring(0, 5));
//			//System.out.println(vlu.substring(6, vlu.length()));
//		}
//		;
//		
//		List<String> list = new ArrayList<String>(map.values());
//		
//
//		System.out.println(treeMap.firstEntry().getValue());
//		System.out.println(treeMap.firstEntry().getKey());
//		System.out.println(treeMap.lastEntry().getValue());
//		System.out.println(treeMap.lastEntry().getKey());
		
	}
	
	
	
}
